﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using YJKArchMethodLibrary.StructurePoleSuit;
using YJKArchMethodLibrary.Utils;

namespace ColumnModuleAndEdit.ColumnModule.DataDefine
{
	public class SectionLoop
	{
		public CurveArray GetEdgeLoopCurves(Application revitApp)
		{
			CurveArray curveArray = revitApp.Create.NewCurveArray();
            foreach (YJKArchMethodLibrary.StructurePoleSuit.Edge edge in this.m_EdgeLoop)
			{
				Curve curve = edge.GetCurve(revitApp);
				curveArray.Append(curve);
			}
			return curveArray;
		}

        public SectionLoop(List<YJKArchMethodLibrary.StructurePoleSuit.Edge> edgeLoop)
		{
			this.m_EdgeLoop = edgeLoop;
		}

		public SectionLoop.ProfileType GetRegionType()
		{
			SectionLoop.ProfileType result = SectionLoop.ProfileType.PT_IRREGULAR;
			if (this.m_EdgeLoop.Count == 4)
			{
                YJKArchMethodLibrary.StructurePoleSuit.Edge edge = this.m_EdgeLoop[0];
                YJKArchMethodLibrary.StructurePoleSuit.Edge edge2 = this.m_EdgeLoop[1];
                YJKArchMethodLibrary.StructurePoleSuit.Edge edge3 = this.m_EdgeLoop[2];
                YJKArchMethodLibrary.StructurePoleSuit.Edge edge4 = this.m_EdgeLoop[3];
				if (edge.IsArc || edge2.IsArc || edge3.IsArc || edge4.IsArc)
				{
					return result;
				}
				if (Geometry.IsVertical(edge.EndPoint - edge.StartPoint, edge2.EndPoint - edge2.StartPoint, 1E-09) && Geometry.IsVertical(edge2.EndPoint - edge2.StartPoint, edge3.EndPoint - edge3.StartPoint, 1E-09) && Geometry.IsVertical(edge3.EndPoint - edge3.StartPoint, edge4.EndPoint - edge4.StartPoint, 1E-09) && Geometry.IsVertical(edge4.EndPoint - edge4.StartPoint, edge.EndPoint - edge.StartPoint, 1E-09))
				{
					return SectionLoop.ProfileType.PT_RECTANGLE;
				}
			}
			else if (this.m_EdgeLoop.Count == 2)
			{
                YJKArchMethodLibrary.StructurePoleSuit.Edge edge5 = this.m_EdgeLoop[0];
                YJKArchMethodLibrary.StructurePoleSuit.Edge edge6 = this.m_EdgeLoop[1];
				if (!edge5.IsArc || !edge6.IsArc)
				{
					return result;
				}
				XYZ xyz = null;
				Geometry.GetCenterWithBulge(edge5.StartPoint, edge5.EndPoint, edge5.Bulge, ref xyz);
				XYZ xyz2 = null;
				Geometry.GetCenterWithBulge(edge6.StartPoint, edge6.EndPoint, edge6.Bulge, ref xyz2);
				if (Geometry.IsEqual((xyz - xyz2).GetLength(), 0.0))
				{
					return SectionLoop.ProfileType.PT_CIRCULAR;
				}
			}
			return result;
		}

        private List<YJKArchMethodLibrary.StructurePoleSuit.Edge> m_EdgeLoop = new List<YJKArchMethodLibrary.StructurePoleSuit.Edge>();

		public enum ProfileType
		{
			PT_RECTANGLE,
			PT_CIRCULAR,
			PT_IRREGULAR
		}
	}
}
